package com.hsgene.user.controller;

import com.hsgene.common.util.resolver.FromSession;
import com.hsgene.common.util.resolver.IgnoreUserToken;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.result.NullObject;
import com.hsgene.common.util.web.session.UserInfo;
import com.hsgene.common.util.web.session.UserSession;
import com.hsgene.user.constants.CustomerSourceType;
import com.hsgene.user.converter.UserConverter;
import com.hsgene.user.dto.login.UserLoginInfoDto;
import com.hsgene.user.dto.login.WeChatBindingRequestDto;
import com.hsgene.user.dto.login.WeChatLoginResponseDto;
import com.hsgene.user.dto.user.GeneticTestingUserDto;
import com.hsgene.user.service.LoginService;
import com.hsgene.user.session.UserSessionComponent;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 登录Controller
 *
 * @author wangbing
 * @version 1.0, 2018/10/16
 */
@RestController
@RequestMapping("/v1.0/genetic/testing")
public class LoginController {

    @Resource
    private LoginService loginService;

    @Resource
    private UserSessionComponent userSessionComponent;

    /**
     * 所有用户登录接口
     *
     * @param loginInfo 登录信息
     * @param session   HttpSession
     * @return 登录结果
     */
    @IgnoreUserToken
    @PostMapping("login")
    public ApiResult<GeneticTestingUserDto> login(
            @RequestBody UserLoginInfoDto loginInfo, HttpSession session) {
        ApiResult<GeneticTestingUserDto> result = loginService.login(loginInfo);
        if (result.isSucc()) {
            UserInfo userInfo = UserConverter.buildUserInfo(result.getData(), loginInfo.parseSourceType());
            UserSession userSession = userSessionComponent.add(session, userInfo);
            result.getData().setUserSession(userSession);
        }
        return result;
    }

    /**
     * 微信code登录
     *
     * @param code 前端获取的微信code
     * @return 登录结果
     */
    @IgnoreUserToken
    @PostMapping("login/webchat")
    public ApiResult<WeChatLoginResponseDto> wechatLogin(
            @RequestParam("code") String code, HttpSession session) {
        ApiResult<WeChatLoginResponseDto> result = loginService.wechatLogin(code);
        if (result.isSucc() && !result.getData().getNeedBinding()) {
            // 已经绑定过手机号，不要重复绑定直接登录
            UserInfo userInfo = UserConverter.buildUserInfo(result.getData().getUser(), CustomerSourceType.WECHAT_APPLICATION);
            UserSession userSession = userSessionComponent.add(session, userInfo);
            result.getData().getUser().setUserSession(userSession);
        }
        return result;
    }

    /**
     * 绑定微信手机号
     *
     * @param bindingRequest 微信绑定请求信息
     * @param session
     * @return 绑定结果
     */
    @IgnoreUserToken
    @PostMapping("login/webchat/binding")
    public ApiResult<GeneticTestingUserDto> wechatBinding(
            @RequestBody WeChatBindingRequestDto bindingRequest, HttpSession session) {
        ApiResult<GeneticTestingUserDto> result = loginService.wechatBinding(bindingRequest);
        if (result.isSucc()) {
            // 绑定成功直接登录
            UserInfo userInfo = UserConverter.buildUserInfo(result.getData(), CustomerSourceType.WECHAT_APPLICATION);
            UserSession userSession = userSessionComponent.add(session, userInfo);
            result.getData().setUserSession(userSession);
        }
        return result;
    }

    /**
     * 用户登出接口
     *
     * @param userInfo 登录用户信息
     * @param session
     * @return 登出结果
     */
    @PostMapping("logout")
    public ApiResult<NullObject> logout(@FromSession UserInfo userInfo, HttpSession session) {
        userSessionComponent.remove(session, userInfo);
        return ApiResult.succ();
    }

    /**
     * 用户更改密码
     *
     * @param userInfo
     * @return
     */
    @PutMapping("password")
    public ApiResult<NullObject> updatePassword(@FromSession UserInfo userInfo) {
        return null;
    }
}
